package com.graduate.bssp.controller;

import com.graduate.base.entity.PageInfo;
import com.graduate.bssp.dao.UserMapper;
import com.graduate.bssp.entity.User;
import com.graduate.bssp.service.IUserService;
import com.graduate.common.constants.MessageConstants;
import com.graduate.common.dto.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import com.graduate.base.controller.BaseController;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.graduate.common.cookieUtil.Util.addCookie;
import static com.graduate.common.cookieUtil.Util.clearCookie;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yongqiangyao
 * @since 2018-02-05
 */
@Controller
@RequestMapping("/bssp/user")
public class UserController extends BaseController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult UserLogin(User user,HttpServletResponse response, HttpServletRequest request){
        AjaxResult ajaxResult=null;
        try {
            User loginUser=userService.selectUser(user);
            if (loginUser!=null) {
                ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, loginUser);
                addCookie(loginUser, response, request);
                return ajaxResult;
            }
            ajaxResult=json(MessageConstants.BSSP_STATUS_FAIL,"用户名或密码错误,请重新输入 ！");
        }
        catch (Exception e){
            e.printStackTrace();
            ajaxResult=result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }
    @RequestMapping("/quit")
    @ResponseBody
    public AjaxResult quitLogin(HttpServletResponse response, HttpServletRequest request){
        AjaxResult ajaxResult=null;
        try {
            clearCookie(response, request);
            ajaxResult=json(MessageConstants.BSSP_STATUS_SUCCESS,"安全退出成功 ！");
            return ajaxResult;
        }
        catch (Exception e){
            e.printStackTrace();
            ajaxResult=json(MessageConstants.BSSP_STATUS_FAIL,"安全退出失败 ！");
        }
        return ajaxResult;
    }
    @RequestMapping(value = "/init")
    @ResponseBody
    public AjaxResult selectAllList2(PageInfo<User> pageInfo1){
        AjaxResult ajaxResult= null ;
        try{
            User user=new User();
            PageInfo<User> pageInfo=new PageInfo<>();
            List<User> userList=userService.selectUserList(pageInfo);
            ajaxResult=json(MessageConstants.BSSP_STATUS_SUCCESS,userList, pageInfo.getDbNumber());
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult=result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }
    @RequestMapping("/adminLogin")
    @ResponseBody
    public AjaxResult AdminLogin(User user,HttpServletResponse response, HttpServletRequest request){
        AjaxResult ajaxResult=null;
        try {
            User loginUser=userService.selectUser(user);
            if (loginUser!=null) {
                ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, loginUser);
                addCookie(loginUser, response, request);
                return ajaxResult;
            }
            ajaxResult=json(MessageConstants.BSSP_STATUS_FAIL,"用户名或密码错误,请重新输入 ！");
        }
        catch (Exception e){
            e.printStackTrace();
            ajaxResult=result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }
}
